#!/bin/sh

Action=$1
IP=$2

#define vars
User="root"
DefaultPWD="1234"
PWD="1234"
RACADM="racadm -r $IP -u $User -p $PWD"

# modify iDrac Admin password
modify_pwd() {
    #racadm -r $IP -u $User -p $DefaultPWD config -g cfgUserAdmin -o cfgUserAdminPassword $PWD -i 2
    # For iDrac 7
    racadm -r $IP -u $User -p $DefaultPWD set iDRAC.Users.2.Password $PWD |grep -Ev 'Security|certificate-related'
    echo -e "$IP password is modified!"
}

# modify BIOS config
modify_bios() {
    $RACADM set bios.sysprofilesettings.SysProfile Custom |grep -Ev 'Security|certificate-related'
    $RACADM set bios.sysprofilesettings.ProcC1E Disabled |grep -Ev 'Security|certificate-related'
    $RACADM set bios.sysprofilesettings.ProcCStates Disabled |grep -Ev 'Security|certificate-related'
    $RACADM set bios.sysprofilesettings.ProcPwrPerf SysDbpm |grep -Ev 'Security|certificate-related'
    $RACADM jobqueue create BIOS.Setup.1-1 |grep -Ev 'Security|certificate-related'
    $RACADM get bios.sysprofilesettings |grep -Ev 'Security|certificate-related'
    echo -e "$IP BIOS is modified!"
}

# Once PXE
once_pxe() {
    $RACADM config -g cfgServerInfo -o cfgServerBootOnce 1 |grep -Ev 'Security|certificate-related'
    $RACADM config -g cfgServerInfo -o cfgServerFirstBootDevice PXE |grep -Ev 'Security|certificate-related'
    $RACADM serveraction powercycle |grep -Ev 'Security|certificate-related'
}

# reboot server by iDrac command tools
iDrac_reboot() {
    $RACADM serveraction powercycle |grep -Ev 'Security|certificate-related'
}

# get info(MAC,SN)
info() {
    TMP="/tmp/.drac.mac"
    #$RACADM getsvctag |grep -Ev 'Security|certificate-related' | head -1
    #$RACADM nicstatistics |grep -Ev 'Security|certificate-related' |grep -E 'NIC.Embedded' |awk '{print $NF}' > $TMP
    $RACADM getsysinfo |grep -Ev 'Security|certificate-related' | grep -E '^Service Tag|^NIC.Integrated' > $TMP
    SN=$(grep '^Service Tag' $TMP | awk -F'=' '{print $2}')
    LANMAC=$(grep 'NIC.Integrated.1-1-1' $TMP | awk -F'=' '{print $2}')
    echo -e "$IP $SN $LANMAC"
}

# Main Process
if [ -z "$IP" ];then
    echo -e "IP is none"
    exit 1
elif [ "$Action" = passwd ];then
    modify_pwd
elif [ "$Action" = bios ];then
    modify_bios
elif [ "$Action" = pxe ];then
    once_pxe
elif [ "$Action" = reboot ];then
    iDrac_reboot
elif [ "$Action" = info ];then
    info
fi
